import requests
from lxml import etree

from captcha_demo import Demo


def getRandCodeText(fileName):
    return Demo(fileName).test()


if __name__ == "__main__":
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    responseText = requests.get("https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx",
                                headers=headers).text
    tree = etree.HTML(responseText)

    with open("./file/RandCode.jpg", "wb") as fp:
        fp.write(
            requests.get("https://so.gushiwen.cn" + tree.xpath("//*[@id='imgCode']/@src")[0], headers=headers).content)

    print("识别结果为：", getRandCodeText("./file/RandCode.jpg"))
